JavaScript 有許多方法可以搭配正規表式是使用,例如 match, replace, search, split, replaceAll等,或使用 exec 去執行比對。在應用中更多細節像是 ?: (passive subexpression)代表只要 group 就好,並不會被單獨拉出作為捕捉。或更常見的處理字串或空白字元之類等例子。
function getOpacity(elem) {
var filter = elem.style.filter;
return filter ?
filter.indexOf("opacity") >= 0 ?
(parseFloat(filter.match(/opacity=([^]+)/)[1]) / 100) + "" : "" : elem.style.opacity;
}
window.onload = function () {
console.log(getOpacity(document.getElementById("opacity")) == "0.5")
}
var all = html.match(/<(\/?)(\w+)([^>]*?)>/g);
var html = "<div class='test'><b>hello</b><i>world!</i></div>";
var tag = /<(\/?)(\w+)([^>]*?)>/g, match;
var num = 0;
while ((match = tag.exec(html)) !== null) {
console.log(match.length == 4)
num++;
}
//?: 為被動子式
var pattern = /((?:ninja-)+)sword/;
var ninjas = "ninja-ninja-sword".match(pattern);
console.log(ninjas.length == 2) //true
"ABCDEfg".replace(/[A-Z]/g, "X");
function upper(all, letter) {
return letter.toUpperCase();
}
"border-bottom-width".replace(/-(\w)/g, upper) == "borderBottomWidth"
foo=1&foo=2&blah=a&blah=b&foo=3
foo=1, 2, 3&blah=a, b
function compress(source) {
var keys = {};
source.replace(
/([^=&]+)=([^&]*)/g,
function(full, key, value) {
keys[key] = (keys[key] ? keys[key] + "," : "") + value;
retrun "";
}
);
var result = [];
for (var key in keys) {
result.push(key + "=" + keys[key]);
}
return result.join("&");
}
function trim(str) {
return (str || "").replace(/^\s+|\s+$/g, "");
}
trim(" #id div.class ") == "#id div.class"
var html = "<b>Hello</b>\n<i>world</i>"
/.*/.exec(html)[0] === "<b>Hello</b>"
/[\S\s]*/.exec(html)[0] === "<b>Hello</b>\n<i>world</i>"
/(?:.|\s)*/.exec(html)[0] === "<b>Hello</b>\n<i>world!</i>"
var text = "\u5FCD\u8005\u30D1\u30EF\u30FC";
var matchAll = /[\w\u0080-\uFFFF_-]+/;
console.log((text).match(matchAll))
var pattern = /^((\w+)|(\\.))+$/;
var test = [
"formUpdate",
"form\\.update\\.whatever",
"form\\:update",
"\\f\\o\\r\\m\\u\\p\\d\\a\\t\\e",
"form:update"
]
for (var n = 0; n < tests.length; n++) {
console.log(pattern.test(tests[n]), tests[n] + 'is a valid identifier');
}
https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Guide/Regular_Expressions
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search
https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/String/match
https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/String/replace
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec